package com.web.base.log.provider;

import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.web.base.log.exception.UploadException;
import com.web.base.log.facade.UploadFacade;
import com.web.base.log.message.UploadRequest;
import com.web.base.log.message.UploadResponse;
import com.web.base.log.model.Upload;
import com.web.base.log.service.UploadService;
import com.web.base.log.util.UploadConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 上传文件日志接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年11月21日
 */
@Component("logUploadProvider")
public class UploadProvider implements UploadFacade
{
	@Autowired
	private UploadService uploadService;
	@Autowired
	private StorageClient storageClient;
	
	public UploadProvider()
	{
	}

	@Override
	public UploadResponse add(UploadRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Upload upload = UploadConvertor.toUpload(request);
		upload = uploadService.save(upload);
		UploadResponse response = UploadConvertor.toResponse(upload, storageClient);
		return response;
	}

	@Override
	public UploadResponse get(Integer id)
	{
		if (id == null)
			throw new UploadException("Id不能为空");
			
		Upload upload = uploadService.get(id);
		UploadResponse response = UploadConvertor.toResponse(upload, storageClient);
		return response;
	}

	@Override
	public Page<UploadResponse> listPage(UploadRequest request, int start, int size)
	{
		Upload upload = UploadConvertor.toUpload(request);
		Page<Upload> page = new Page<>(size);
		page.setCurPage(start);
		page = uploadService.list(upload, page);
		Page<UploadResponse> pageResponse = UploadConvertor.toResponse(page, storageClient);
		return pageResponse;
	}
	
}
